﻿using Org.BouncyCastle.Crypto;
using ProTrack.Entity;
using ProTrack.IRepository;
using ProTrack.IServices.Works;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace ProTrack.Services.Works
{
    public class WorkshopServices : IWorkshopServices
    {
        public WorkshopServices(IBaseRepository db)
        {
            Db = db;
        }
        public IBaseRepository Db { get; }

        public async Task<bool> AddAsync(Workshop model)
        {
            var result = await Db.InsertAsync(model);
            return result;
        }

        public async Task<bool> DeleteAsync(int[] Ids)
        {
            string sql = "delete Workshop where Id in @Ids";
            var result = await Db.DeleteAsync(sql, new { Ids });
            return result;
        }

        public async Task<bool> DeleteAsync(int Id)
        {
            string sql = "delete Workshop where Id = @Id";
            var result = await Db.DeleteAsync(sql, new { Id });
            return result;
        }

        public async Task<IEnumerable<Workshop>> GetAllAsync()
        {
            string sql = "select * from Workshop";
            var list = await Db.QueryAllAsync<Workshop>(sql);
            return list;
        }

        public async Task<IEnumerable<Workshop>> GetAsync(int PageIndex, int PageSize, string WorkshopCode, string WorkshopName, string Charge)
        {
            string sql = "select * from Workshop where 1=1";
            if (!string.IsNullOrEmpty(WorkshopCode))
            {
                sql += " and WorkshopCode like @WorkshopCode ";
            }
            if (!string.IsNullOrEmpty(WorkshopName))
            {
                sql += " and WorkshopName like @WorkshopName ";
            }
            if (!string.IsNullOrEmpty(Charge))
            {
                sql += " and Charge like @Charge ";
            }

            sql += " order by CreateTime desc offset @PageSize*(@PageIndex-1) row fetch next @PageSize rows only ";

            var list = await Db.QueryAsync<Workshop>(sql, new { PageIndex, PageSize, WorkshopCode = $"%{WorkshopCode}%", WorkshopName = $"%{WorkshopName}%", Charge = $"%{Charge}%" });
            return list;
        }

        public async Task<int> GetCountAsync(string WorkshopCode, string WorkshopName, string Charge)
        {
            string sql = "select count(*) from Workshop where 1=1";
            if (!string.IsNullOrEmpty(WorkshopCode))
            {
                sql += " and WorkshopCode like @WorkshopCode ";
            }
            if (!string.IsNullOrEmpty(WorkshopName))
            {
                sql += " and WorkshopName like @WorkshopName ";
            }
            if (!string.IsNullOrEmpty(Charge))
            {
                sql += " and Charge like @Charge ";
            }
            var list = await Db.GetRecoreCountAsync(sql, new {WorkshopCode = $"%{WorkshopCode}%", WorkshopName = $"%{WorkshopName}%", Charge = $"%{Charge}%" });
            return list;
        }

        public async Task<bool> UpdateAsync(Workshop model)
        {
            var result = await Db.UpdateAsync(model);
            return result;
        }
    }
}
